﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Web.Security;
using System.IO;

namespace WebshopPizzaRio
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        PizzaBLL pbbl;
        int pizzaid;
        protected void Page_Load(object sender, EventArgs e)
        {
            pbbl = new PizzaBLL();
            Samenstellen samenstellen = (Samenstellen)Session["samenstellen"];
            if (samenstellen.GetandSetIseersteStapGedaan)
            {
                ingredienten_lv.DataSource = samenstellen.getSamenstellenArray;
                ingredienten_lv.DataBind();
                RadioButtonList2.Visible = false;
                Label1.Visible = false;
                Label2.Font.Bold = true;
                Label3.Text = UpdateTotaalPrijs();
                bt_Samenstelopslaan.Enabled = true;

                if (!Page.IsPostBack)
                {
                    DDL_selectingredient.Width = 200;
                    DDL_selectingredient.AppendDataBoundItems = true;
                    DDL_selectingredient.DataTextField = "naam";
                    DDL_selectingredient.DataValueField = "ingredientnr";
                    DDL_selectingredient.Items.Insert(0, new ListItem("--Selecteer een ingredient--", "--Selecteer een ingredient--"));
                    DDL_selectingredient.DataSource = pbbl.getIngredienten();
                    DDL_selectingredient.AutoPostBack = true;
                    DDL_selectingredient.SelectedIndex = 0;
                    DDL_selectingredient.DataBind();
                }
            }
            else
            {
                Response.Redirect("Pizza-samenstellen-intro.aspx");
            }
        }

        protected void DDL_selectingredient_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DDL_selectingredient.SelectedIndex != 0)
            {
                ListView1.DataSource = pbbl.getIngredientenBYNR(int.Parse(DDL_selectingredient.SelectedValue.ToString()));
                ListView1.DataBind();
                Label1.Visible = true;
                RadioButtonList2.Visible = true;
            }
            else
            {
                ListView1.DataBind();
            }
        }

        protected void ListView1_SelectedIndexChanging(object sender, ListViewSelectEventArgs e)
        {
            ListView1.SelectedIndex = e.NewSelectedIndex;

            Samenstellen samenstellen = (Samenstellen)Session["samenstellen"];
            if (samenstellen.GetSetAantalIngredienten < 10)
            {
                Ingredient ing = samenstellen.getNewItem();
                ing.Ingredientnr = int.Parse(ListView1.SelectedDataKey.Values["ingredientnr"].ToString());
                ing.Ingredientnaam = ListView1.SelectedDataKey.Values["naam"].ToString();
                ing.ingredientprijs = Decimal.Parse(ListView1.SelectedDataKey.Values["prijs per kg"].ToString());

                if (RadioButtonList2.SelectedValue == "weinig")
                {
                    ing.hoeveelheid = "weinig";
                }
                else if (RadioButtonList2.SelectedValue == "veel")
                {
                    ing.hoeveelheid = "veel";
                }
                else if (RadioButtonList2.SelectedValue == "normaal")
                {
                    ing.hoeveelheid = "normaal";
                }

                samenstellen.AddIngredient(ing);
                ingredienten_lv.DataBind();
                ListView1.DataBind();

                DDL_selectingredient.SelectedIndex = 0;
                RadioButtonList2.SelectedIndex = 1;

                RadioButtonList2.Visible = false;
                Label1.Visible = false;
                Label3.Text = UpdateTotaalPrijs();
            }
            else
            {
                Label2.Visible = true;
            }
        }

        protected void ingredienten_lv_SelectedIndexChanging(object sender, ListViewSelectEventArgs e)
        {
            Samenstellen samenstellen = (Samenstellen)Session["samenstellen"];
            ingredienten_lv.SelectedIndex = e.NewSelectedIndex;

            if (samenstellen.GetSetAantalIngredienten > 0)
            {
                Ingredient removeingredient = samenstellen.getNewItem();
                removeingredient.Ingredientnr = int.Parse(ingredienten_lv.SelectedDataKey.Values["ingredientnr"].ToString());
                samenstellen.DeleteItem(removeingredient);
                ingredienten_lv.DataBind();
                ListView1.DataBind();
                Label2.Visible = false;
                Label3.Text = UpdateTotaalPrijs();
            }
        }

        private String UpdateTotaalPrijs()
        {
            Samenstellen samenstellen = (Samenstellen)Session["samenstellen"];
            String prijs = "Totaalprijs: €" + samenstellen.GetTotaalPrijs();
            return prijs;
        }

        protected void bt_Samenstelopslaan_Click(object sender, EventArgs e)
        {
            MembershipUser currentuserid = Membership.GetUser();
            Guid userid;
            
            try
            {
                if ((Guid)currentuserid.ProviderUserKey == null)
                {
                    throw new NullReferenceException();
                }
                else
                {
                    userid = (Guid)currentuserid.ProviderUserKey;
                    if (pbbl.getAantalSamenGesteldePizza(userid) <= 4)
                    {
                        Samenstellen samenstellen = (Samenstellen)Session["samenstellen"];
                        pbbl = new PizzaBLL();

                        userid = (Guid)currentuserid.ProviderUserKey;

                        pizzaid = getNewPizzaID();
                        int publiekelijk;

                        if (samenstellen.GetandSetPubliekelijk == "0")
                        {
                            publiekelijk = 0;
                        }
                        else
                        {
                            publiekelijk = 1;
                        }

                        pbbl.InsertPizza(pizzaid, samenstellen.GetandSetPizzaNaam, samenstellen.GetTotaalPrijs(), 1, publiekelijk, userid);

                        foreach (Ingredient ingredient in samenstellen.getSamenstellenArray)
                        {
                            pbbl.InsertPizzaRegel(pizzaid, ingredient.Ingredientnr, ingredient.hoeveelheid);
                        }

                        lbl_deeplinkpizza.Visible = true;
                        txb_deeplinkpizza.Visible = true;
                        txb_deeplinkpizza.Text = "http://localhost:1258/Pizzadetails.aspx?id=" + pizzaid;
                        txb_deeplinkpizza.Width = 300;

                        samenstellen.GetandSetIseersteStapGedaan = false;
                        samenstellen.GetandSetPizzaNaam = String.Empty;
                        samenstellen.GetandSetPubliekelijk = "1";
                        samenstellen.getSamenstellenArray.Clear();
                        bt_Samenstelopslaan.Enabled = false;
                    }
                    else
                    {
                        Label5.Visible = true;
                    }
                }
            }
            catch (NullReferenceException)
            {
                Label4.Visible = true;
            }
        }

        private int getNewPizzaID()
        {
            pbbl = new PizzaBLL();
            int pizzaid = int.Parse(pbbl.getNewPizzaID().ToString()) +1;
            return pizzaid;
        }
    }
}